package endurance.observer.listener;

import endurance.observer.Observer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: sunlin
 * @Date: 2024-05-17 11:14
 * @Version: 1.0
 * @Description: 创建一个集合，监听所有的Observer
 */

@Component
public class ListenerObserver {

    private List<Observer> observerList = new ArrayList<>();

    private ExecutorService executorService;

    public ListenerObserver() {
        executorService = Executors.newFixedThreadPool(10);
    }

    public void addObserver(Observer observer) {
        observerList.add(observer);
    }

    public void notifyObservers(HashMap<String, String> hashMap) {
        for (Observer observer : observerList) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    observer.sendMessage(hashMap);
                }
            });
        }
    }

}
